package ren.steve.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

/**
 * @Author: CIS-T-13 Liu GuoJia
 * @Description:
 * @Date: Created in 2024/2/7 16:36
 */
@Configuration
public class RedisConfig {
  @Bean
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);

    // 设置键的序列化策略
    template.setKeySerializer(new StringRedisSerializer());

    // 设置值的序列化策略，这里使用 Jackson 的序列化方式
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

    // 设置 Hash 的 key 和 value 的序列化方式
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

    return template;
  }
}
